﻿#if !LITE
using System.IO;
using System.Diagnostics;

using Microsoft.Win32;

using Core = Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;


namespace iPdf
{
    
    internal class OfficeChecker
    {
        private const string RegKey = @"Software\Microsoft\Windows\CurrentVersion\App Paths";
        private const int OfficeVersionNumber =
#if OFFICE_2007
        12;
#else
        11;
#endif

        private static bool? wordExists = true;
        private static bool? excelExists = null;
        private static bool? powerPointExists = null;

        public static bool WordExist
        {
            get
            {
                try
                {
                    if (wordExists != null)
                    {
                        return (bool)wordExists;
                    }

                    string wordPath = GetComponentPath(OfficeComponent.Word);
                    wordExists = GetMajorVersion(wordPath) == OfficeVersionNumber;

                    return (bool)wordExists;
                }
                catch
                {
                    wordExists = false;
                    return false;
                }
            }
        }

        public static bool ExcelExist
        {
            get
            {
                try
                {
                    if (excelExists != null)
                    {
                        return (bool)excelExists;
                    }

                    string excelPath = GetComponentPath(OfficeComponent.Excel);
                    excelExists = GetMajorVersion(excelPath) == OfficeVersionNumber;

                    return (bool)excelExists;
                }
                catch
                {
                    excelExists = false;
                    return false;
                }
            }
        }

        public static bool PowerPointExist
        {
            get
            {
                try
                {
                    if (powerPointExists != null)
                    {
                        return (bool)powerPointExists;
                    }

                    string pptPath = GetComponentPath(OfficeComponent.PowerPoint);
                    powerPointExists = GetMajorVersion(pptPath) == OfficeVersionNumber;

                    return (bool)powerPointExists;
                }
                catch
                {
                    powerPointExists = false;
                    return false;
                }
            }
        }

        /// <summary>
        /// gets the component's path from the registry. if it can't find it - retuns an empty string
        /// </summary>
        /// <param name="_component"></param>
        /// <returns></returns>
        private static string GetComponentPath(OfficeComponent _component)
        {
            string toReturn = string.Empty;
            string _key = string.Empty;

            switch (_component)
            {
                case OfficeComponent.Word:
                    _key = "winword.exe";
                    break;
                case OfficeComponent.Excel:
                    _key = "excel.exe";
                    break;
                case OfficeComponent.PowerPoint:
                    _key = "powerpnt.exe";
                    break;
            }

            //looks inside LOCAL_MACHINE:
            RegistryKey _mainKey = Registry.LocalMachine;
            try
            {
                _mainKey = _mainKey.OpenSubKey(RegKey + "\\" + _key, false);
                if (_mainKey != null)
                {
                    toReturn = _mainKey.GetValue(string.Empty).ToString();
                }
            }
            catch
            { }

            //if not found, looks inside CURRENT_USER:
            _mainKey = Registry.CurrentUser;
            if (string.IsNullOrEmpty(toReturn))
            {
                try
                {
                    _mainKey = _mainKey.OpenSubKey(RegKey + "\\" + _key, false);
                    if (_mainKey != null)
                    {
                        toReturn = _mainKey.GetValue(string.Empty).ToString();
                    }
                }
                catch
                {
                }
            }

            //closing the handle:
            if (_mainKey != null)
                _mainKey.Close();

            return toReturn;
        }

        /// <summary>
        /// Gets the major version of the path. if file not found (or any other exception occures
        /// - returns 0
        /// </summary>
        private static int GetMajorVersion(string _path)
        {
            int toReturn = 0;
            if (File.Exists(_path))
            {
                try
                {
                    FileVersionInfo _fileVersion = FileVersionInfo.GetVersionInfo(_path);
                    toReturn = _fileVersion.FileMajorPart;
                }
                catch
                { }
            }

            return toReturn;
        }

        public enum OfficeComponent
        {
            Word,
            Excel,
            PowerPoint
        }

    }
    
}

#endif